package com.design.patterns.behavioralModel.builder.bean;

public class UserBuilder extends User {
    /**
     * 无参数构造
     */
    public UserBuilder(){
    }

    public UserBuilder name(String name){
        super.setName(name);
        return this;
    }
    public UserBuilder password(String password) {
        super.setPassword(password);
        return this;
    }

    public UserBuilder nickName(String nickName) {
        super.setNickName(nickName);
        return this;
    }

    public UserBuilder age(int age) {
        super.setAge(age);
        return this;
    }
    public User build() {
        //校验判断
        if (super.getName() == null || super.getPassword() == null) {
            throw new RuntimeException("用户名和密码必填");
        }
        if (super.getAge() <= 0 || super.getAge() >= 150) {
            throw new RuntimeException("年龄不合法");
        }
        // 设置默认值
        if (super.getNickName() == null) {
            super.setNickName(super.getName());
        }
        return this;
    }
}

